package cn.yitun.chat.yitunservermanagecore.general.manage.common.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupHeadDataDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupHeadDataVo;
import cn.yitun.chat.yitunservermanagecore.general.manage.common.service.GroupHeadDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 群组头部数据表
 * 
 * @author chenyitun
 * @email chenyitun@163.com
 * @date 2020-05-27 14:42:53
 */
 
@RestController
@RequestMapping("/common/groupHeadData")
public class GroupHeadController extends BaseController {

	@Autowired
	private GroupHeadDataService groupHeadDataService;

    /**
    * 列表
	*/
    @PostMapping("/page")
    public Object list(JwtUserDto userDto, @JsonDefine GroupHeadDataDto groupHeadDataDto){
        //查询列表数据
        checkRootPage(groupHeadDataDto , userDto);
        PageHelper.startPage(groupHeadDataDto.getCurrentPageNo() , groupHeadDataDto.getPageSize());
        List<GroupHeadDataVo> list = groupHeadDataService.listVO(groupHeadDataDto);
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("page", new PageInfo<>(list));
        return rm;
    }

	/**
	 * 保存
	 */
	@PostMapping("/save")
	public Object save(JwtUserDto userDto, @JsonDefine GroupHeadDataDto groupHeadData){
        setUserMsgSave(groupHeadData, userDto);
		if( groupHeadDataService.save(groupHeadData) == 0)
            throw new YitunException(ExceptionEnum.ADD_FAIL);

		return new ResultMessage();
	}
	/**
	 * 修改
	 */
	@PostMapping("/update")
	public Object update(JwtUserDto userDto, @JsonDefine  GroupHeadDataDto groupHeadData){
        setUserMsgUpdate(groupHeadData, userDto);
        if (groupHeadData.getId() == null)
            throw new YitunException(ExceptionEnum.LOSE_FILED);

	    if( groupHeadDataService.update(groupHeadData) == 0 )
            throw new YitunException(ExceptionEnum.EDIT_FAIL);

		return new ResultMessage();
	}
	
	/**
	 * 删除
	 */
	@DeleteMapping( "/remove/{id}")
	public Object remove(@PathVariable("id") Long id){
		if( groupHeadDataService.remove(id) == 0 )
            throw new YitunException(ExceptionEnum.DELETE_FAILED);

		return new ResultMessage();
	}
	
	/**
	 * 批量删除
	 */
	@DeleteMapping( "/batchRemove/{ids}")
	public Object remove(@PathVariable Long[] ids){
        if( groupHeadDataService.batchRemove(ids) == 0 )
            throw new YitunException(ExceptionEnum.DELETE_FAILED) ;

		return new ResultMessage();
	}

}
